<?php
/** 命名空间 */
namespace app\cp;

//$GLOBALS['cp']['session']			  	  = 'cp_hy'; 	//管理id
//$GLOBALS['cp']['session_pass']		  = $GLOBALS['cp']['session'].'_pass';
//$GLOBALS['cp']['session_cid']		      = $GLOBALS['cp']['session'].'_cid';
//$GLOBALS['cp']['session_type']	  	  = $GLOBALS['cp']['session'].'_type';
//$GLOBALS['cp']['session_hash']		  = $GLOBALS['cp']['session'].'_hash';
/**
 * 是否登录
 * @return boolean
 */
function login_is()
{
	$hash = login_key_rule($_SESSION[$GLOBALS['cp']['session_cid']], $_SESSION[$GLOBALS['cp']['session']], $_SESSION[$GLOBALS['cp']['session_pass']]);
	if (	$_SESSION[$GLOBALS['cp']['session']] &&
		 	$_SESSION[$GLOBALS['cp']['session_hash']] == $hash) 
	{
		return true;
	}
	return false;
}

/**
 * 登录
 * $field : id,type,cid,account,password,note
 */
function login(\ounun\Mysql $db,$cid,$account,$password)
{
	$check  = login_check($db, $cid, $account);
	// 封帐号或IP
	if(!$check[0])
	{
		$status  = 0;
		login_logs($db,$status,$cid,$account);
		return $check;
	}
	// 正常登录
	$rs 	= $db->row('select `id`,`type`,`cid`,`account`,`password`,`login_times` from `hyapi_cp` where `account` =:account and `cid` = :cid limit 1;',array('account'=>$account,'cid'=>$cid));
	//print_r($rs);
	//exit();
	//$password   	= $account;
	//$rs['type'] 	= 10;
	//$rs['cid']  	= $cid;
	//$rs['password'] = md5($password);
	if($rs)
	{
		if( $rs['password'] == md5($password) )
		{
			// 清理一下
			login_out(); 
			// 登录时设一下
			set_cookie_cid($cid);
			// 设定session
			$_SESSION[$GLOBALS['cp']['session']]  	   = $account;
			$_SESSION[$GLOBALS['cp']['session_cid']]   = $cid;
			$_SESSION[$GLOBALS['cp']['session_hash']]  = login_key_rule($cid,$account,$password);
			$_SESSION[$GLOBALS['cp']['session_pass']]  = $password;
			$_SESSION[$GLOBALS['cp']['session_type']]  = $rs['type'];
			// 返回
			$login_times	= $rs['login_times'] + 1;
			$login_last		= time();
			$id				= $rs['id'];
			$bind	= array(
					'login_times'=>$login_times,
					'login_last'=>$login_last,
			);
			$db->update('`hyapi_cp`', $bind,' `id`= :id',$id);
			$status  = 1;
			login_logs($db,$status,$cid,$account);
			return array(true);
		}else
		{
			$status  = 0;
			login_logs($db,$status,$cid,$account);
			return array(false,'提示:帐号或密码有误');
		}		
	}else 
	{
		$status  = 0;
		login_logs($db,$status,$cid,$account);
		return array(false,'提示:帐号不存在');	
	}
}
/**
 * 记下登录日志
 * @param \ounun\Mysql $db
 * @param int $status 返回状态 0:失败 1:成功
 * @param int $cid	合作方ID
 * @param string $account 用户帐号
 */
function login_logs(\ounun\Mysql $db, $status, $cid, $account)
{
	$ip		= \ounun\ip();
	$wry    = new \plugins\qqwry\QQWry('utf-8');
	$uCity  = $wry->getlocation($ip);
// 	var_dump($uCity);	
// 	exit();
	
	$time		= time();
	$ip_segment	= $uCity["beginip"] . "-" . $uCity["endip"];
	$address	= $uCity["country"];
	#$db		= new \ounun\Mysql($GLOBALS['scfg']['db']['core']);
	$bind	= array(
			'time'=>$time,
			'status'=>$status,
			'cid'=>$cid,
			'account'=>$account,
			'ip'=>$ip,
			'ip_segment'=>$ip_segment,
			'address'=>$address,
	);
	$db->insert('`hyapi_logs_login_cp`', $bind);
	#exit();
}
/**
 * IP检查
 * @param \ounun\Mysql $db
 * @param int $cid
 * @param string $account
 */
function login_check(\ounun\Mysql $db, $cid, $account)
{
	$ip		= \ounun\ip();
	$wry    = new \plugins\qqwry\QQWry('utf-8');
	$uCity  = $wry->getlocation($ip);
	$ip_segment	= $uCity["beginip"] . "-" . $uCity["endip"];
	$status	=0;
	$time	= time() - 86400;
	$rs_ip_segment_counts	= $db->rows('select * from `hyapi_logs_login_cp` where `ip_segment` =:ip_segment and `status` =:status and `time` >=:time ;',array('ip_segment'=>$ip_segment,'status'=>$status,'time'=>$time));
	if ($rs_ip_segment_counts <= Const_Cp_IPs_Count)
	{
		$rs_ip_counts	= $db->rows('select * from `hyapi_logs_login_cp` where `ip` =:ip and `status` =:status and `time` >=:time;',array('ip'=>$ip,'status'=>$status,'time'=>$time));
		if ($rs_ip_counts <= Const_Cp_IP_Count)
		{
			return array(true);
		}else
		{
			return array(false,'IP地址登录失败超过'.Const_Cp_IP_Count.'次');
		}
	}else
	{
		return array(false,'IP地址段登录失败超过'.Const_Cp_IPs_Count.'次');
	}
}
/**
 * 退出登录
 */
function login_out()
{
	$_SESSION[$GLOBALS['cp']['session']]  	   = '';
	$_SESSION[$GLOBALS['cp']['session_cid']]   = '';
	$_SESSION[$GLOBALS['cp']['session_hash']]  = '';
	$_SESSION[$GLOBALS['cp']['session_pass']]  = '';
	$_SESSION[$GLOBALS['cp']['session_type']]  = '';
}
/**
 * 生成Hash
 * @param int $cid
 * @param string $account
 * @param string $password
 */
function login_key_rule($cid, $account, $password)
{
	return sha1($account.$cid. $GLOBALS['cp']['session'] . $password);
}
/**
 * 得到用户组名
 */
function get_group_name($type=null)
{
	if (!$type)
	{
		$type  = $_SESSION[$GLOBALS['cp']['session_type']];
		if(!$type){
			return false;
		}	
	}	
	return $GLOBALS['cp']['purview_list'][$type];
}
/**
 * 得到管理员帐号
 */
function get_account()
{
	return $_SESSION[$GLOBALS['cp']['session']];
}
/**
 * 帐号权限类型
 */
function get_type()
{
	return $_SESSION[$GLOBALS['cp']['session_type']];
}
/**
 * 帐号所属平台
 */
function get_cid()
{
	return (int)$_SESSION[$GLOBALS['cp']['session_cid']];
}
function set_cookie_cid($cid)
{
	if(!get_cid())
	{
		setcookie('cp_cid', $cid, time()+86400);
	}
}
function get_cookie_cid()
{
	$cid  = get_cid();
	if($cid)
	{
		return $cid;
	}
	return $_COOKIE['cp_cid'];
}
function set_cookie_sid($sid)
{
	setcookie('cp_sid', $sid, time()+86400);
}
function get_cookie_sid()
{
	return (int)$_COOKIE['cp_sid'];
}
/**
 * 获得权限目录
 * @return multitype:
 */
function purview_data()
{
	$purview	= array();
	if (login_is())
	{
		$type   = $_SESSION[$GLOBALS['cp']['session_type']];
		foreach ($GLOBALS['cp']['purview'] as $key1 => $data1)
		{
			$purview_sub	= array();
			foreach ($data1['sub'] as $key2 => $data2)
			{
				if($data2['key'])
				{
					if(in_array($type, $data2['key']))
					{
						unset($data2['key']);
						$purview_sub[$key2] = $data2;
					}
				}else
				{
					$purview_sub2	= array();
					foreach ($data2['data'] as $key3 => $data3)
					{
						if($data3['key'] && in_array($type, $data3['key']) )
						{
							unset($data3['key']);
							$purview_sub2[$key3] = $data3;
						}
					}
					if ($purview_sub2)
					{
						$purview_sub[$key2] = array(
								'name'	=> $data2['name'],
								'data'	=> $purview_sub2,
							);
					}
				}
			}
			// 
			if($purview_sub)
			{
				$purview[$key1] = array(
						'name' 		=> $data1['name'],
						'default'	=> $data1['default'],
						'sub'		=> $purview_sub,
					);
			}			
		}		
	}
	return $purview;
}
/**
 * 权限检测
 * @param string $key
 * @return boolean
 */
function purview_check($key)
{
	$type  = $_SESSION[$GLOBALS['cp']['session_type']];
	if(!$type){
		return false;
	}
	if('tree@root' == $key){
		return in_array($type, $GLOBALS['cp']['purview_tree_root']);
	}elseif('tree@coop' == $key){
		return in_array($type, $GLOBALS['cp']['purview_tree_coop']);
	}else {
		$key	= explode('@', $key);
		$data	= $GLOBALS['cp']['purview'][$key[0]];
		if($data && $data['sub'])
		{
			$sub	= 	$data['sub'][$key[1]];
			if($sub && $sub['key'])
			{
				return in_array($type, $sub['key']);
			}
			foreach ($data['sub'] as $subs)
			{
				if ($subs && is_array($subs) && !$subs['key'])
				{
					$sub	= 	$subs['data'][$key[1]];
					if($sub && $sub['key'])
					{
						return in_array($type, $sub['key']);
					}
				}
			}				
		}
	}
	return false;
}
